﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class TrainPlanAndContentStudyRateController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        [HttpGet]
        [Route("api/TrainPlanAndContentStudyRate/TrainPlanStudyRate")]
        public IHttpActionResult TrainPlanStudyRate(string swhere, int page, int limit)
        {
            string TPID = "";
            string searchLevel = "";
            if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                swhere = string.Empty;
                foreach (var item in jsonObject)
                {
                    if (item.Key == "TPID")
                    {
                        if (item.Value != "")
                            TPID = item.Value;
                    }
                    if (item.Key == "searchLevel")
                    {
                        if (item.Value != "")
                            searchLevel = item.Value;
                    }
                }
            }

            ResultMsg resultMsg = new ResultMsg();
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            try
            {
                if ( String.IsNullOrEmpty( TPID ))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "请选择计划";
                    resultMsg.Data = null;
                }
                if (resultMsg.Info != "" && resultMsg.Info != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Unauthorized;
                    resultMsg.Info = "您没有权限或计划未选择！";
                    resultMsg.Data = null;
                }
                else
                {
                    bool blExist = false;
                    DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.V_CourseStudyUserInfo().TrainPlanStudyRate(Convert.ToInt16(TPID), (int)LoginUser.DepID, Convert.ToInt16(searchLevel),(int)LoginUser.DepLevel); ;
                    if (dsSearch != null)
                        if (dsSearch.Tables.Count > 0)
                            if (dsSearch.Tables[0].Rows.Count > 0)
                            {
                                //返回token信息
                                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                                resultMsg.Data = dsSearch.Tables[0];
                                resultMsg.Count = dsSearch.Tables[0].Rows.Count;
                                blExist = true;
                            }
                    if (!blExist)
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = "您没有权限或计划未选择！";
                        resultMsg.Data = null;
                    }
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }


        [HttpGet]
        [Route("api/TrainPlanAndContentStudyRate/TrainContentStudyRate")]
        public IHttpActionResult TrainContentStudyRate(string swhere, int page, int limit)
        {
            string TPID = "";
            string TContent = "";
            string searchLevel = "";
            if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                swhere = string.Empty;
                foreach (var item in jsonObject)
                {
                    if (item.Key == "TPID")
                    {
                        if (item.Value != "")
                            TPID = item.Value;
                    }
                    if (item.Key == "TContent")
                    {
                        if (item.Value != "")
                            TContent = item.Value;
                    }
                    if (item.Key == "searchLevel")
                    {
                        if (item.Value != "")
                            searchLevel = item.Value;
                    }
                }
            }

            ResultMsg resultMsg = new ResultMsg();
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            try
            {
                if (String.IsNullOrEmpty(TContent))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "请选择内容";
                    resultMsg.Data = null;
                }
                if (resultMsg.Info != "" && resultMsg.Info != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Unauthorized;
                    resultMsg.Info = "您没有权限或内容未选择！";
                    resultMsg.Data = null;
                }
                else
                {
                    bool blExist = false;
                    DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.V_CourseStudyUserInfo().TrainContentStudyRate(TContent, (int)LoginUser.DepID, Convert.ToInt16(searchLevel), (int)LoginUser.DepLevel); ;
                    if (dsSearch != null)
                        if (dsSearch.Tables.Count > 0)
                            if (dsSearch.Tables[0].Rows.Count > 0)
                            {
                                //返回token信息
                                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                                resultMsg.Data = dsSearch.Tables[0];
                                resultMsg.Count = dsSearch.Tables[0].Rows.Count;
                                blExist = true;
                            }
                    if (!blExist)
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = "您没有权限或内容未选择";
                        resultMsg.Data = null;
                    }
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }



    }
}
